package com.example.mathtest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class LoveQuestionActivity extends AppCompatActivity {

    Toolbar toolbar ;
    ListView listView;
    ArrayList<Question> lovequestionList = new ArrayList<>();   //收藏的题目;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_question);
        toolbar = findViewById(R.id.toolbar_love);
        toolbar.setNavigationIcon(R.mipmap.ic_keyboard_arrow_left_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                finish();
            }
        });
        listView = findViewById(R.id.LoveListView);

        if (LoveLoad() == 0){
            //创建返回的对话框
            AlertDialog.Builder isReturn = new AlertDialog.Builder(this);
            //设置标题
            isReturn.setTitle("消息提醒");
            //设置对话框的消息
            isReturn.setMessage("你还没有收藏题目呢！先去做题吧！");
            //设置按钮并进行监听
            isReturn.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    LoveQuestionActivity.this.finish();
                }
            });
            isReturn.show();
        }
        else {
            LoveQuestionsAdapter loveQuestionsAdapter = new LoveQuestionsAdapter(LoveQuestionActivity.this,R.layout.item2,lovequestionList);
            listView.setAdapter(loveQuestionsAdapter);
        }
    }


    //读取收藏夹列表
    private int LoveLoad (){
        String path = this.getFilesDir().getPath() + "/LoveQuestions.json";
        File file = new File(path);
        BufferedReader reader = null ;
        String loveload = "";

        //判断有无文件存在
        if (!file.exists()){
            return 0;
        }

        //读取文件存放在String loveload中
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while((tempString = reader.readLine())!=null){
                loveload += tempString;
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //将String loveload 转为列表
        System.out.println(loveload);
        lovequestionList  = JSON.parseObject(loveload, new TypeReference<ArrayList<Question>>(){});

        return lovequestionList.size();
    }

}
